TESTSERVER=testserver
CLIENT=client
TESTCLIENT=testclient

#DIR=$(shell pwd)
DIR=.
SRCPATH=$(DIR)/src
SRC=$(SRCPATH)/shdev.c $(SRCPATH)/shdevmag.c $(SRCPATH)/uart.c $(SRCPATH)/shsocket.c
SRC += $(SRCPATH)/pcduino.c
OBJS=$(patsubst %.c, %.o, $(SRC))
TESTPATH=$(DIR)/test
LIB_PATH=$(DIR)/lib
LIB_STATIC=libshdevmag.a
LIB_SHARE=libshdevmag.so
LIB=$(LIB_STATIC) $(LIB_SHARE)
INCPATH=$(DIR)/include
INCLUDE=-I$(INCPATH)
CFLAGS=-g -fPIC
#CFLAGS=-g -fPIC -DPCDUINO
LDFLAGS=-g

%.o: %.c
	@rm -f $@
	$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@

ALL= $(LIB) $(TESTSERVER) $(CLIENT) $(TESTCLIENT)
all: $(ALL)

$(LIB):$(OBJS)
	mkdir -p $(LIB_PATH) $(INCPATH) 
	$(AR) cq $(LIB_PATH)/$(LIB_STATIC) $^
	$(CC)  -shared -Wl,-soname,$(LIB_SHARE) -o $(LIB_PATH)/$(LIB_SHARE) $^
	cp -f $(SRCPATH)/shdev.h $(INCPATH)/

$(TESTSERVER):$(TESTPATH)/testserver.c $(LIB_PATH)/$(LIB_STATIC)
	$(CC) $(CFLAGS) $(INCLUDE) -L$(LIB_PATH) -o $@ $^

$(CLIENT):$(TESTPATH)/client.c
	$(CC) $(CFLAGS) -L$(LIB_PATH) -o $@ $<

$(TESTCLIENT):$(TESTPATH)/testclient.c
	$(CC) $(CFLAGS) -L$(LIB_PATH) -o $@ $<

clean:
	@rm -f $(ALL) *~ *.o
	@rm -f $(SRCPATH)/*.o
	@rm -f $(LIB_PATH)/*
